﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-4-26
/// Purpose: 		Url工具类，提供Url处理的静态方法
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2013-11-4       增加GetQuery方法
/// Robin           2015-2-13       修复AppendPath的Bug
/// Robin           2015-2-28       改进AppendQuery
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RB.Utility
{
    /// <summary>
    /// Url工具类，提供Url处理的静态方法
    /// </summary>
    public static class UrlUtility
    {
        /// <summary>
        /// 在Uri中追加路径
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="path"></param>
        public static string AppendPath(string url, string path)
        {
            if (url == null)
                throw new ArgumentNullException("url");

            if (path == null)
                throw new ArgumentNullException("path");

            if (path.StartsWith("~"))
            {
                path = path.Substring(1);
            }
            path = Uri.EscapeUriString(path);
            if (!url.EndsWith("/") && !path.StartsWith("/"))
            {
                url += "/";
            }
            return url + path;
        }

        /// <summary>
        /// 在Uri后追加一个参数
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static string AppendQuery(string url, string name, object value)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentException();

            if (value != null)
            {
                string queryToAppend = Uri.EscapeUriString(name) + "=" + Uri.EscapeUriString(value.ToString());
                if (url.Contains('?'))
                {
                    if (!url.EndsWith("?"))
                    {
                        url += '&';
                    }
                }
                else
                    url += '?';
                return url + queryToAppend;
            }
            return url;
        }

        public static string RemoveQuery(string url, string name)
        {
            Regex reg = new Regex("&?" + name + "=[^&#]*");
            return reg.Replace(url, "");
        }

        public static string RemoveQuery(string url)
        {
            int i = url.IndexOf('?');
            if (i >= 0)
            {
                url = url.Remove(i);
            }
            if (url.Length > 1 && url.EndsWith("/"))
            {
                url = url.Remove(url.Length - 1);
            }
            return url;
        }

        public static string GetQuery(string url)
        {
            int i = url.IndexOf('?');
            if (i >= 0)
            {
                return url.Substring(i);
            }
            return null;
        }
    }
}
